import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { SFSchema, SFUISchema, SFComponent } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-alain-table-edit',
  templateUrl: './edit.component.html'
})
export class AlainTableEditComponent implements AfterViewInit {
  @ViewChild('sf', { static: false })
  sf!: SFComponent;

  record: any = {};
  i: any;
  schema: SFSchema = {
    properties: {}
  };
  ui: SFUISchema = {};

  constructor(private modal: NzModalRef, private msgSrv: NzMessageService, public http: _HttpClient) {}

  ngAfterViewInit(): void {
    this.http.get(`api/build`).subscribe(res => {
      this.sf.refreshSchema(res.schema, res.ui);
    });
    if (this.record.id > 0) this.http.get(`/user/${this.record.id}`).subscribe(res => (this.i = res));
  }

  save(value: any): void {
    let a = this.sf.validator();
    debugger;
    console.info(value);
    this.http.post(`/user/${this.record.id}`, value).subscribe(res => {
      this.msgSrv.success('保存成功');
      this.modal.close(true);
    });
  }

  close(): void {
    this.modal.destroy();
  }
}
